package com.sdyeda.readiot.fx;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sdyeda.readiot.config.Setting;
import com.sdyeda.readiot.data.SetData;
import com.sdyeda.readiot.data.SetDataService;
import com.sdyeda.readiot.iotinfo.IotInfo;
import com.sdyeda.readiot.iotinfo.JdbcIotInfo;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;

@Component
public class SetIotController {

	private Stage stage;
	
	@FXML
	private Button btnSave,btnClose,btnTestConnect,btnQuery;
	
	@FXML
	private TextField txtUrl,txtUsername,txtPassword;
	
	@FXML
	private TextArea txtGetiotinfo;
	
	@FXML
	private ChoiceBox<String> cbDriverClassName;
	
	@FXML
	private TableView<IotInfo> tvIotinfo;
	
	@Autowired
	private SetDataService setservice;
	
	private Map<String,SetData> sets;
	
	@FXML
	public void initialize() {
		
		//现只支持Mysql
		cbDriverClassName.getItems().add("Mysql");
		
		//按钮事件
		btnSave.setOnAction(activeExent->{
			if(SavaData()) {
				new Alert(AlertType.INFORMATION, "保存成功！").show();
				stage.close();
			}
		});
		btnClose.setOnAction(actionEvent->{
			stage.close();
		});
		btnTestConnect.setOnAction(actionEvent ->{
			if(TestConnect()) {
				new Alert(AlertType.INFORMATION, "连接成功！").show();
			}
			else {
				new Alert(AlertType.ERROR, "连接失败！").show();
			}
		});
		btnQuery.setOnAction(actionEvent->{
				
			List<IotInfo> list = QueryIotinfo();
			if(list==null) {
				new Alert(AlertType.ERROR, "没有获得传感器数据！").show();
			}
			else {
				ObservableList<IotInfo> tvlist = FXCollections.observableArrayList(list);
				tvIotinfo.setItems(tvlist);
				tvIotinfo.refresh();
			}
		});
		
		
		LoadData();
	}
	
	private List<IotInfo> QueryIotinfo() {
		// TODO Auto-generated method stub
		return JdbcIotInfo.getall(getDriverclassname(), txtUrl.getText(),txtUsername.getText(),txtPassword.getText(),txtGetiotinfo.getText());
	}

	private boolean TestConnect() {
		return JdbcIotInfo.TestConnent(getDriverclassname(), txtUrl.getText(),txtUsername.getText(),txtPassword.getText());
	}
	
	private String getDriverclassname() {
		String driverclassname = "";
		if(cbDriverClassName.getValue().equals("Mysql")) {
			driverclassname = "com.mysql.cj.jdbc.Driver";	
		}
		return driverclassname;
	}

	private boolean SavaData() {
		SetData value;
		
		//Driver-Class-Name
		value = sets.get(Setting.DATASOURCE_DRIVER);
		value.setValue(getDriverclassname());
		setservice.save(value);
		
		value = sets.get(Setting.DATASOURCE_URL);
		value.setValue(txtUrl.getText());
		setservice.save(value);
		
		value = sets.get(Setting.DATASOURCE_USERNAME);
		value.setValue(txtUsername.getText());
		setservice.save(value);
		
		value = sets.get(Setting.DATASOURCE_PASSWORD);
		value.setValue(txtPassword.getText());
		setservice.save(value);
		
		value = sets.get(Setting.SQL_GETIOTINFO);
		value.setValue(txtGetiotinfo.getText());
		setservice.save(value);
		
		return true;
	}

	void LoadData() {
		sets = setservice.getAllSetData();
		
		txtUsername.setText(sets.get(Setting.DATASOURCE_USERNAME).getValue());
		txtPassword.setText(sets.get(Setting.DATASOURCE_PASSWORD).getValue());
		txtUrl.setText(sets.get(Setting.DATASOURCE_URL).getValue());
		txtGetiotinfo.setText(sets.get(Setting.SQL_GETIOTINFO).getValue());
		
		if(sets.get(Setting.DATASOURCE_DRIVER).getValue().equals("com.mysql.cj.jdbc.Driver")) {
			cbDriverClassName.setValue("Mysql");
		}
		
	}

	public Stage getStage() {
		return stage;
	}

	public void setStage(Stage stage) {
		this.stage = stage;
	}
	

}
